package com.emm.yixun.mobile.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.emm.yixun.mobile.R;
import com.emm.yixun.mobile.model.GetHousesList.PremisesList;
import com.eroad.product.tools.MyGridView;

/**
 * 
 * <p>
 * <b>Title：</b> 房源适配器
 * </p>
 * <p>
 * <b>Description：</b>
 * </p>
 * 
 * 
 * @author tangxin
 * @date 2016年12月13日 下午6:18:17
 */
public class MainHousingFragmentAdapter extends BaseAdapter {

	ArrayList<PremisesList> list;
	Context context;

	public MainHousingFragmentAdapter(ArrayList<PremisesList> list, Context context) {
		this.list = list;
		this.context = context;
	}

	public void SetDate(ArrayList<PremisesList> list) {
		this.list = list;
		notifyDataSetChanged();
	}

	@Override
	public int getCount() {
		return list == null ? 0 : list.size();
	}

	@Override
	public Object getItem(int position) {
		return list == null ? 0 : list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHoudler v = null;
		if (convertView == null) {
			convertView = LayoutInflater.from(context).inflate(R.layout.housing_resources, null);
			v = new ViewHoudler();
			v.gridview_housing_item = (MyGridView) convertView.findViewById(R.id.gridview_housing_item);
			v.notSoldNum_view = (TextView) convertView.findViewById(R.id.notSoldNum_view);
			v.premisesName_view = (TextView) convertView.findViewById(R.id.premisesName_view);
			v.soldNum_view = (TextView) convertView.findViewById(R.id.soldNum_view);
			convertView.setTag(v);
		} else {
			v = (ViewHoudler) convertView.getTag();
		}

		HousingMyGridViewAdapter adapter = new HousingMyGridViewAdapter(context, list.get(position).getBuildingList(),
				list.get(position).getPremisesName());
		v.gridview_housing_item.setAdapter(adapter);
		if ("".equals(list.get(position).getNotSoldNum()) || "null".equals(list.get(position).getNotSoldNum())
				|| TextUtils.isEmpty(list.get(position).getNotSoldNum())) {
			v.notSoldNum_view.setText("0");
		} else {
			if (Double.valueOf(list.get(position).getNotSoldNum()).doubleValue() > 99) {
				v.notSoldNum_view.setText("99+");
			} else {
				v.notSoldNum_view.setText(list.get(position).getNotSoldNum());
			}
		}

		if ("".equals(list.get(position).getSoldNum()) || "null".equals(list.get(position).getSoldNum())
				|| TextUtils.isEmpty(list.get(position).getSoldNum())) {
			v.soldNum_view.setText("0");
		} else {
			if (Double.valueOf(list.get(position).getSoldNum()).doubleValue() > 99) {
				v.soldNum_view.setText("99+");
			} else {
				v.soldNum_view.setText(list.get(position).getSoldNum());
			}
		}

		v.premisesName_view.setText(list.get(position).getPremisesName());

		return convertView;
	}

	public static class ViewHoudler {

		TextView premisesName_view;

		TextView notSoldNum_view;

		TextView soldNum_view;

		MyGridView gridview_housing_item;
	}

}